create or replace function updatePosterminal(
	out xml, inout xml
	)
as
$$
	declare
		-- alias is name for stored procedure parameters
		executeStatusData 	alias for $1;
		posterminalData		alias for $2;
		
		vIdPosterminal 		bigint := null; -- posterminal id in database
		vSerialNumber		text := null; -- posterminal serial number
		vInventoryNumber	text := null; -- posterminal inventory number
		vIdModel			bigint := null;
		vIdCurPoslocation	bigint := null;
		vIdConnectionType	integer := null;
		
		
		vIdNode				varchar(100) := '/posterminal/@id';
		vSerialNode			varchar(100) := '/posterminal/serialnumber';
		vInventoryNode		varchar(100) := '/posterminal/inventorynumber';
		vIdProducerNode		varchar(100) := '/posterminal/posterminalmodel/@id';
		
	begin
		vIdPosterminal := cast(extract_value(vIdNode, posterminalData) as bigint);
		vSerialNumber := extract_value(vSerialNode, posterminalData);
		vInventoryNumber := extract_value(vInventoryNode, posterminalData);
		vIdModel := extract_value(vIdProducerNode, posterminalData);
		
		-- first insert into trace table previous data
		insert into posterminalTrace(idPosTerminal, serialNumber, inventoryNumber, idModel, idCurrentPosLocation, idConnectionType)
			select idPosTerminal, serialNumber, inventoryNumber, idModel, idCurrentPosLocation, idConnectionType from posterminal
				where
					posterminal.idPosTerminal = vIdPosterminal
		;		
		
		-- update actual data
		
		update PosTerminal poal
			set 
				serialNumber 			= vSerialNumber, 
				inventoryNumber 		= vInventoryNumber,
				idModel					= vIdModel,
				idCurrentPosLocation 	= vIdCurPoslocation,
				idConnectionType		= vIdConnectionType
			where
				poal.idPosTerminal			= vIdPosterminal;
				
		
		executeStatusData := statusCall(successcode(), updateText());

	end;
$$ language plpgsql VOLATILE;